package com.wzy.utils;

import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;

/*
 * ������  
 *   1����ߴ��븴����
 *   2�����Ӳ����
 */
public class JDBCUtils {
	
	private static String driver;
	private static String url;
	private static String username;
	private static String password;
	static {

		try {
			//文件字节输入流
			FileInputStream fis = new FileInputStream("src/main/resources/db.properties");
			//创建Properties
			Properties pro = new Properties();
			//{}
			pro.load(fis);
			//{driver=com.mysql.jdbc.Driver,url=jdbc:mysql:///day09,username=root,password=root}
			//根据key获取值
			driver = pro.getProperty("driver");
			url = pro.getProperty("url");
			username = pro.getProperty("username");
			password = pro.getProperty("password");

			Class.forName(driver);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	
	/*
	 * ��ȡ����
	 */
	
	public static Connection getConnection() {
		
		try {
			return DriverManager.getConnection(url,username,password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	/*
	 * �ر���Դ
	 */
	public static void closeResouces(ResultSet rs,Statement st,Connection con) {
		
		if(rs!=null) {
			
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		if(st!=null) {
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		if(con!=null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}
